import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  UseGuards,
  Req,
  HttpStatus,
  HttpCode,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
import { TaskCommentReplyService } from './task-comment-reply.service';
import { 
  CreateCommentReplyDto, 
  UpdateCommentReplyDto,
  CommentReplyResponseDto 
} from './dto';
import { Permissions } from '../../decorators/permissions.decorator';
import { PermissionsGuard } from '../../guards/permissions.guard';
import { plainToClass } from 'class-transformer';

@ApiTags('评论回复')
@Controller('replies')
@UseGuards(JwtAuthGuard, PermissionsGuard)
@ApiBearerAuth()
export class TaskCommentReplyController {
  constructor(private readonly taskCommentReplyService: TaskCommentReplyService) {}

  @Post()
  @Permissions('task:comment:reply:create')
  @ApiOperation({ summary: '创建评论回复' })
  @ApiResponse({ 
    status: HttpStatus.CREATED, 
    description: '回复创建成功', 
    type: CommentReplyResponseDto 
  })
  async createReply(
    @Body() createCommentReplyDto: CreateCommentReplyDto,
    @Req() req,
  ) {
    const userId = req.user.id;
    const reply = await this.taskCommentReplyService.createReply(
      createCommentReplyDto,
      userId,
    );
    return plainToClass(CommentReplyResponseDto, reply);
  }

  @Get('comment/:commentId')
  @Permissions('task:comment:reply:read')
  @ApiOperation({ summary: '获取评论的所有回复' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取成功', 
    type: [CommentReplyResponseDto]
  })
  async getCommentReplies(@Param('commentId') commentId: string) {
    const replies = await this.taskCommentReplyService.getCommentReplies(commentId);
    return replies.map(reply => plainToClass(CommentReplyResponseDto, reply));
  }

  @Get(':id')
  @Permissions('task:comment:reply:read')
  @ApiOperation({ summary: '获取回复详情' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '获取成功', 
    type: CommentReplyResponseDto 
  })
  async getReply(@Param('id') id: string) {
    const reply = await this.taskCommentReplyService.getReplyById(id);
    return plainToClass(CommentReplyResponseDto, reply);
  }

  @Put(':id')
  @Permissions('task:comment:reply:update')
  @ApiOperation({ summary: '更新回复' })
  @ApiResponse({ 
    status: HttpStatus.OK, 
    description: '更新成功', 
    type: CommentReplyResponseDto 
  })
  async updateReply(
    @Param('id') id: string,
    @Body() updateCommentReplyDto: UpdateCommentReplyDto,
    @Req() req,
  ) {
    const userId = req.user.id;
    const reply = await this.taskCommentReplyService.updateReply(
      id,
      updateCommentReplyDto,
      userId,
    );
    return plainToClass(CommentReplyResponseDto, reply);
  }

  @Delete(':id')
  @Permissions('task:comment:reply:delete')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: '删除回复' })
  @ApiResponse({ status: HttpStatus.NO_CONTENT, description: '删除成功' })
  async deleteReply(
    @Param('id') id: string,
    @Req() req,
  ) {
    const userId = req.user.id;
    await this.taskCommentReplyService.deleteReply(id, userId);
  }
} 